package com.ebelter.myehc.ui.fragments;


import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.models.bluetooth.products.three2one.ThreeOneManager;
import com.ebelter.btcomlib.models.bluetooth.products.three2one.bean.ThreeOneMesureResultBean;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.views.LevelColorView;
import com.ebelter.btcomlib.views.WheelView;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.btcomlib.views.dialogs.WheelViewDialog;
import com.ebelter.myehc.R;
import com.ebelter.myehc.models.http.upload.UpLoadUtil;
import com.ebelter.myehc.ui.fragments.modifys.ModifyAddInfo_Fragment;
import com.ebelter.myehc.ui.view.CombinView5;
import com.ebelter.myehc.utils.ViewUtilsEHC;
import java.util.ArrayList;
import java.util.List;

import static com.ebelter.btcomlib.bases.activity.BaseActivity.getString_;
import static com.ebelter.btcomlib.views.dialogs.TextTipDialog.Gravity_BUTTOM;

/**
 * 血糖
 */

public class BG_Fragment extends BaseFragment {


    public static final String TAG = "BG_Fragment";
    public static final String TIME_SELECT = "TIME_SELECT";
    LevelColorView bg_lcv;
    CombinView5 cb5_bg;

    TextView time_sp_tv;


    private TextView activityTopTittleTv;
    public void setActivityTopTittleTv(TextView activityTopTittleTv) {
        this.activityTopTittleTv = activityTopTittleTv;
    }


    private void updateTime_sp_tv(){
        //timeSelect = 0;//0空腹 1餐后
        if (timeSelect==0){
            ViewUtils.setTextViewStr(time_sp_tv,getString_(R.string.kongfu));
        }else {
            ViewUtils.setTextViewStr(time_sp_tv,getString_(R.string.canhuo));
        }
    }

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_bg;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();
    }

    @Override
    protected void initData(Bundle arguments) {
        super.initData(arguments);
        ViewUtilsEHC.setCb5Title2(cb5_bg,N);

        timeSelect= UserSpUtil.readInt(TIME_SELECT);
        updateTime_sp_tv();
    }

    private void FV() {
        bg_lcv = mRootView.findViewById(R.id.bg_lcv);
        cb5_bg = mRootView.findViewById(R.id.cb5_bg);
        time_sp_tv = mRootView.findViewById(R.id.bg_time_tv);
    }

    @Override
    protected void setListener() {
        super.setListener();

        time_sp_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //  showTimeSp();
                showSpDialog();
            }
        });
    }


   private int timeSelect = 0;//0空腹 1餐后



    //展示天周月年对话框
    private void showSpDialog() {
        if (getContext()==null){
            return;
        }
        ModifyAddInfo_Fragment.setTextImage(time_sp_tv,R.drawable.ic_shangla);

        WheelViewDialog wheelViewDialog = new WheelViewDialog(getContext());
        wheelViewDialog.setGravity(Gravity_BUTTOM);


        final List<String> list=new ArrayList<>();
        list.add(getString_(R.string.kongfu));
        list.add(getString_(R.string.canhuo));

        wheelViewDialog.setOffeset(1);
        wheelViewDialog.setItems(list);
        wheelViewDialog.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                Log.i(TAG, "selectedIndex: " + selectedIndex + ", item: " + item);
                timeSelect = selectedIndex-1;
            }
        });
        wheelViewDialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                Log.i(TAG, "cancel");
                dialogView.cancel();
            }

            @Override
            public void ok(Dialog dialogView) {
                Log.i(TAG, "ok");
                ViewUtils.setTextViewStr(time_sp_tv,list.get(timeSelect));
                UserSpUtil.writeInt(TIME_SELECT,timeSelect);

                dialogView.cancel();



            }
        });

        wheelViewDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                ModifyAddInfo_Fragment.setTextImage(time_sp_tv,R.drawable.ic_xiala);
            }
        });

        wheelViewDialog.setSelectPosition(timeSelect);

        wheelViewDialog.show();



    }

    public void setMesureResult(ThreeOneMesureResultBean mesureResult) {
        if (cb5_bg!=null) cb5_bg.setTitle2(mesureResult.mesureValue+"");

        //>0良好 1一般 2偏低 3不良
        int result = ThreeOneManager.computerBGResult(timeSelect, NumUtils.numBaoLiuWei1(mesureResult.mesureValue));
        int result2=1;//0偏低，1良好，2一般，3不良（偏高）
        if (result==0){
            result2=1;
        }else if (result==1){
            result2=2;
        }else if (result==2){
            result2=0;
        }else if (result==3){
            result2=3;
        }

        ViewUtils.displayView(cb5_bg);
        if (bg_lcv!=null)bg_lcv.setLevel(result2);


        String resultDes=ThreeOneManager.computerBGResultDesc(result);
        ViewUtils.setTextViewStr(activityTopTittleTv,resultDes);

        //timeSelect=0空腹 =1 餐后
        if (timeSelect==0){
            mesureResult.type=1;
        }else {
            mesureResult.type=3;
        }
        UpLoadUtil.getInstance().upLoadThreeOne(true,mesureResult);

    }

    public void setMesureResult2Null() {
        if (cb5_bg!=null) cb5_bg.setTitle2(N);
        ViewUtils.goneView(bg_lcv);
        ViewUtils.setTextViewStr(activityTopTittleTv,getString_(R.string.xuetang));
    }

    public void add(String authKey) {
        //三合一 血糖
        ThreeOneMesureResultBean san0 = new ThreeOneMesureResultBean();
        san0.authKey = authKey;
        san0.product = 1;
        san0.mesureValue = 5.6f;//01血糖 6.2~7.0 mmol/L一般   3.9～6.1mmol/L良好  ＞7.0mmol/L不良   ＜3.9mmol/L偏低
        san0.mesureTimeStr="2019-05-14 15:19:23";
       // timeSelect=0空腹 =1 餐后
       // type = 1空腹 2餐前 3餐后
        if (timeSelect==0){
            san0.type=1;
        }else {
            san0.type=3;
        }
        UpLoadUtil.getInstance().upLoadThreeOne(true, san0);
    }
}
